package com.mrtan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.Order;
import com.mrtan.bean.*;
import com.mrtan.dto.*;
import com.mrtan.dto.YearsDto;
import com.mrtan.mapper.*;
import com.mrtan.service.OrderService;
import com.mrtan.vo.*;
import com.mrtan.vo.PurchaseOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class  OrderServiceImpl implements OrderService {
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private StockMapper stockMapper;
    @Resource
    private ProductMapper productMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private DetailsMapper detailsMapper;
    @Resource
    private SupplierMapper supplierMapper;

    @Resource
    private SupplierProductMapper supplierProductMapper;
    @Resource
    private InandoutMapper inandoutMapper;


    @Override
    public void addSell(OrderInsertVO orderInsertVO) {
        String uuid = StrUtil.uuid();
        orderInsertVO.setOrdercode(uuid);
        BigDecimal totalPrice = BigDecimal.ZERO; // 初始化销售总价为0
        for (Object productInfo : orderInsertVO.getProducts()) {
            Map<String, Object> productMap = (Map<String, Object>) productInfo;
            String numberStr = (String) productMap.get("number");
            int number = Integer.parseInt(numberStr);
            BigDecimal sellingPrice = new BigDecimal(productMap.get("sellingPrice").toString());
            // 计算单个产品的销售金额
            BigDecimal productTotalPrice = sellingPrice.multiply(BigDecimal.valueOf(number));
            // 累加到总销售金额
            totalPrice = totalPrice.add(productTotalPrice);
        }
        orderInsertVO.setPrice(totalPrice);
        int i = orderMapper.insertOrderSell(orderInsertVO);
        OrderSell orderSell = orderMapper.selectByOrderCode(uuid);
        for (Object productInfo : orderInsertVO.getProducts()) {
            Map<String, Object> productMap = (Map<String, Object>) productInfo;
            String numberStr = (String) productMap.get("number");
            Integer number = Integer.parseInt(numberStr);
            int i1=detailsMapper.insertBySell(orderSell.getId(), (Integer) productMap.get("productId"), number);
        }
    }

    @Override
    public List<OrderSell> orderList(OrderShowWhereVO orderShowWhereVO) {
        List<OrderSell> orders = orderMapper.selectAllSell(orderShowWhereVO);
        for (OrderSell order : orders) {
            if (order.getType() == 0) {
                Customer customer = customerMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(customer);
            } else if (order.getType() == 1) {
                Supplier supplier = supplierMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(supplier);
            }
        }
        return orders;
    }

    @Override
    public List<OrderProcure> orderProductList(OrderProcureVO orderProcureVO) {
        if(orderProcureVO.getStatus()==-1){
            orderProcureVO.setStatus(null);
        }
        List<OrderProcure> orders = orderMapper.selectAllProcure(orderProcureVO);
        if(!orders.isEmpty()){
            for (OrderProcure order : orders) {
                if (order.getType() == 0) {
                    Supplier supplier = supplierMapper.selectByPrimaryKey(order.getWhoId());
                    order.setObject(supplier);
                } else if (order.getType() == 1) {
                    Supplier supplier = supplierMapper.selectByPrimaryKey(order.getWhoId());
                    order.setObject(supplier);
                    List<Details> details = order.getDetails();
                    for (Details detail : details) {
                        SupplierProduct ishave = supplierProductMapper.ishave(detail.getProduct().getId(), supplier.getId());
                       if(ishave!=null){
                           detail.setSupplyPrice(ishave.getSupplyPrice());
                           detail.setSuggestedPrice(ishave.getSuggestedPrice());
                       }
                    }
                }

            }
        }
        return orders;
    }

    @Override
    public List<OrderSell> orderListw() {
        List<OrderSell> orders = orderMapper.selectAllSellw();
        for (OrderSell order : orders) {
            if (order.getType() == 0) {
                Customer customer = customerMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(customer);
            } else if (order.getType() == 1) {
                Supplier supplier = supplierMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(supplier);
            }
        }
        return orders;
    }

    @Override
    public List<OrderSell> orderListy() {
        List<OrderSell> orders = orderMapper.selectAllSelly();
        for (OrderSell order : orders) {
            if (order.getType() == 0) {
                Customer customer = customerMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(customer);
            } else if (order.getType() == 1) {
                Supplier supplier = supplierMapper.selectByPrimaryKey(order.getWhoId());
                order.setObject(supplier);
            }
        }
        return orders;
    }


    @Override
    public void addOrder(OrderVO orderVO) {

    }

    @Override
    public List<OrderSell> findAllOrder() {
        List<OrderSell> list = orderMapper.selectList();
        return list;
    }

    @Override
    @Transactional
    public void addPurchase(PurchaseOrderVO purchaseOrderVO) {
        String uuid = StrUtil.uuid();
        purchaseOrderVO.setOrdercode(uuid);
        orderMapper.insertPurchase(purchaseOrderVO);
        OrderSell orderSell = orderMapper.selectByOrderCode(uuid);
        List<ProductInfo> productInfo = purchaseOrderVO.getProductInfo();
        for (ProductInfo info : productInfo) {
            detailsMapper.insertByPurchase(info.getProduct(),info.getQuantity(),orderSell.getId());
        }
    }

    @Override
    public FinancialDto findFinancial(int year) {
        List<YearDto> idto = inandoutMapper.selectfinanciali(year);
        List<YearDto> odto = inandoutMapper.selectfinancialo(year);

        FinancialDto dto = new FinancialDto();
        dto.setExpenditure(idto);
        dto.setSales(odto);
        return dto;
    }

    @Override
    public List<Integer> findFinancialyear() {
        List <Integer> list = inandoutMapper.selectAllyear();
        return list;
    }

    @Override
    public HomeDto showhome() {
        HomeDto homeDto = new HomeDto();
        List<InandOutDto> procure = inandoutMapper.findinandout(1);
        List<InandOutDto> sell = inandoutMapper.findinandout(0);
        Info p= orderMapper.findOrderInfo(1);
        Info s= orderMapper.findOrderInfo(0);
        InandOutInfo iinfo = inandoutMapper.findinandoutInfo(1);
        InandOutInfo oinfo = inandoutMapper.findinandoutInfo(0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String year = sdf.format(date);

        List<YearDto> phome = orderMapper.findhome(year,1);
        List<YearDto> shome = orderMapper.findhome(year,0);
        homeDto.setProcureInfo(p);
        homeDto.setSellInfo(s);
        homeDto.setProcure(procure);
        homeDto.setSell(sell);
        homeDto.setSellnum(oinfo);
        homeDto.setProcurenum(iinfo);
        homeDto.setSellorder(shome);
        homeDto.setProcureorder(phome);
        return homeDto;
    }

    @Override
    public OrderSell findbyorderId(int order) {

        OrderSell orderSell = orderMapper.selectByPrimaryKey(order);
        if (orderSell.getType() == 0) {
            Supplier supplier = supplierMapper.selectByPrimaryKey(orderSell.getWhoId());
            orderSell.setObject(supplier);
        } else if (orderSell.getType() == 1) {
            Supplier supplier = supplierMapper.selectByPrimaryKey(orderSell.getWhoId());
            orderSell.setObject(supplier);
            List<Details> details = orderSell.getDetails();
            for (Details detail : details) {
                SupplierProduct ishave = supplierProductMapper.ishave(detail.getProduct().getId(), supplier.getId());
                if(ishave!=null){
                    detail.setSupplyPrice(ishave.getSupplyPrice());
                    detail.setSuggestedPrice(ishave.getSuggestedPrice());
                }
            }
        }
        return orderSell;
    }

    @Override
    public List<InandOutInfoDto> findinanoutbyorder(int order) {
        List<InandOutInfoDto> findbyorderid = inandoutMapper.findbyorderid(order);
        return findbyorderid;
    }

    @Override
    public int updateOrderStatus(OrderVO orderVO) {
        return orderMapper.updateStatus(orderVO);
    }
    @Override
    public int updateSell(OrderUpdateWhoVo orderUpdateWhoVo) {
        Customer customer = customerMapper.selectByName(orderUpdateWhoVo.getName());
        return orderMapper.updateSell(orderUpdateWhoVo.getOrdercode(), customer.getId());
    }

    @Override
    public BigDecimal getAllPrice() {
        BigDecimal i = orderMapper.selectAllPrice();
        return i;
    }

    @Override
    public int getAllOrderCount() {
        int i = orderMapper.selectAllOrderCount();
        return i;
    }

    @Override
    public List<YearsDto> findOrderCountAndPrice(String year) {
        List<YearsDto> list = orderMapper.selectCountAndPrice(year);
        return list;
    }


}
